<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *订单管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$return['status']=$this->jiqilog_status;
$tabs=array(
	array('op'=>'list','title'=>'答题记录'),
); 
if($_RQ['op']=='list'){
	$return['params']=array();
	$params=array('uniacid'=>$_SESSION['uniacid'],'issub'=>1);
	if($_RQ['mid']){
		$params['mid']=$_RQ['mid'];
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['id']){
		$params['datiid']=$_RQ['id'];
		$return['params']['id']=$_RQ['id'];
	}
	if($_RQ['dayid']){
		$params['dayid']=$_RQ['dayid'];
		$return['params']['dayid']=$_RQ['dayid'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$list=pdo_getslice('jy_lineks_canyu',$params,array($pindex,$psize),$total,$fields,'','createtime desc');
	if($list){
		$nowyear=date('Y');
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['starttime']=$item['starttime']?:$item['createtime'];
			if($item['endtime']){
				$item['istime']=$item['endtime']-$item['starttime'];
				if($item['istime']>60){
					$item['istimefen']=intval($item['istime']/60);
					$item['istimemin']=$item['istime']-$item['istimefen']*60;
				}else{
					$item['istimefen']=0;
					$item['istimemin']=$item['istime'];
				}
				$item['istimeinfo']=$item['istimefen'].'分钟'.$item['istimemin'].'秒';
			}
			$item['datatitle']=pdo_getcolumn('jy_lineks_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['datiid']),'title');
			$birth=pdo_getcolumn('jy_lineks_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'birth');
			$birth=json_decode($birth,true);
			$item['birth']=$birth['year'].'-'.$birth['month'].'-'.$birth['day'];
			if($birth['year']){
				$item['age']=$nowyear-$birth['year'];
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'name','title'=>'姓名','type'=>'string'),
			array('name'=>'mobile','title'=>'电话','type'=>'string'),
			array('name'=>'age','title'=>'年龄','type'=>'string'),
			array('name'=>'birth','title'=>'出生年月日','type'=>'string'),
			array('name'=>'datatitle','title'=>'答卷','type'=>'string'),
			array('name'=>'dayid','title'=>'答卷日期','type'=>'string'),
			array('name'=>'score','title'=>'个人得分','type'=>'string'),
			array('name'=>'istimeinfo','title'=>'答卷用时','type'=>'string'),
			array('name'=>'allscore','title'=>'总分数','type'=>'string'),
			array('name'=>'starttime','title'=>'答卷时间','type'=>'time'),
			array('name'=>'endtime','title'=>'交卷时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'href','op'=>'down','title'=>'导出答卷'),
		)),
		'search'=>array(
			array('name'=>'dayid','title'=>'日期','type'=>'string','width'=>15),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		)
	);
	$return['htmls']=$htmls;	
	exi($return);
}
if($_RQ['op']=='down'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('下载出错！','error');
	}
	$url=download_url('jy_lineks/canyu/down_load/id='.$id);
	exi('点确定开始下载','',$url);
}
if($_RQ['op']=='down_load'){
	$id=intval($_RQ['id']);
	$this->model('model')->down_canyu($id);
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('jy_lineks_canyu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				pdo_delete('jy_lineks_canyu',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='export'){
	$params=array('uniacid'=>$_SESSION['uniacid'],'issub'=>1);
	if($_RQ['mid']){
		$params['mid']=$_RQ['mid'];
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['id']){
		$params['datiid']=$_RQ['id'];
		$return['params']['id']=$_RQ['id'];
	}
	if($_RQ['dayid']){
		$params['dayid']=$_RQ['dayid'];
		$return['params']['dayid']=$_RQ['dayid'];
	}
	$list=pdo_getall('jy_lineks_canyu',$params,$fields,'','score desc,istime asc');
	if($list){
		$nowyear=date('Y');
		foreach($list as $k=>&$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['nickname']=$item['member']['nickname'];
			$item['starttime']=$item['starttime']?:$item['createtime'];
			if($item['endtime']){
				$item['istime']=$item['endtime']-$item['starttime'];
				if($item['istime']>60){
					$item['istimefen']=intval($item['istime']/60);
					$item['istimemin']=$item['istime']-$item['istimefen']*60;
				}else{
					$item['istimefen']=0;
					$item['istimemin']=$item['istime'];
				}
				$item['istimeinfo']=$item['istimefen'].'分钟'.$item['istimemin'].'秒';
			}
			$item['datatitle']=pdo_getcolumn('jy_lineks_list',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['datiid']),'title');
			$item['starttime']=date('Y-m-d H:i:s', $item['starttime']);
			$item['endtime']=date('Y-m-d H:i:s', $item['endtime']);
			$birth=pdo_getcolumn('jy_lineks_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']),'birth');
			$birth=json_decode($birth,true);
			$item['birth']=$birth['year'].'-'.$birth['month'].'-'.$birth['day'];
			if($birth['year']){
				$item['age']=$nowyear-$birth['year'];
			}
		}
	}
	foreach($list as $k=>&$nv){
			$nv['key']=$nv['rank']=$k+1;
	}
	$header=array(
			'key'=>'排名',
			'nickname'=>'会员信息',
			'name'=>'姓名',
			'mobile'=>'电话',
			'age'=>'年龄',
			'birth'=>'出生年月日',
			'datatitle'=>'答卷',
			'score'=>'个人得分',
			'istimeinfo'=>'答卷用时',
			'allscore'=>'总分数',
			'starttime'=>'答卷时间',
			'endtime'=>'交卷时间',
	);
	cfc('tools')->export2excel($header,$list,'答题记录');
	exi($return);
}